﻿using SHomeWorkshop.LunarPresentation.Interfaces;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 逆向选取内部公式或清除文本块文本。
        /// </summary>
        public static void SelectBackInOrClearBaseText()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null)
            {
                if (Globals.selectedEditor.RootPanel != null)
                {
                    Globals.selectedEditor.SelectedExpression
                        = Globals.selectedEditor.RootPanel;
                }

                return;
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                Expressions.SubPanel subPanel = curEx as Expressions.SubPanel;
                if (subPanel.BasePanel.Children.Count > 0)
                {
                    if (subPanel.BasePanel.Children[subPanel.BasePanel.Children.Count - 1]
                        is Expressions.Expression)
                    {
                        Expressions.Expression destEx = subPanel.BasePanel.Children[subPanel.BasePanel.Children.Count - 1]
                            as Expressions.Expression;

                        //因为最后一个公式可能被折叠了。
                        //没有必要修改Enter键“选进”的代码——因为首公式是永远不会被某个标题给隐藏起来的。
                        if (destEx.Visibility == System.Windows.Visibility.Visible)
                        {
                            curEx.Editor.SelectedExpression = destEx;
                        }
                        else
                        {
                            int destIndex = subPanel.BasePanel.Children.IndexOf(destEx);
                            Expressions.Expression newDestEx = null;
                            for (int i = destIndex - 1; i >= 0; i--)
                            {
                                Expressions.Expression ex = subPanel.BasePanel.Children[i] as Expressions.Expression;
                                if (ex.Visibility == System.Windows.Visibility.Visible)
                                {
                                    newDestEx = ex;
                                    break;
                                }
                            }

                            if (newDestEx != null)
                            {
                                curEx.Editor.SelectedExpression = newDestEx;
                            }
                        }
                    }
                }
                return;
            }

            //看看是否被折叠起来，如果是被折叠起来，就展开。
            //注意，支持ICollapsed接口的公式里，扩展式是比较特殊的——它不完全折叠。
            if (curEx is ICollapsed)
            {
                if (curEx is Expressions.Expander == false)
                {
                    ICollapsed collapsedEx = curEx as ICollapsed;

                    if (collapsedEx.IsCollapsed)
                    {
                        ModifingItem mi = new ModifingItem();

                        Action actIsCollapsed = new Action(curEx.ExID, -1, curEx.GetType().Name,
                            "IsCollapsed", collapsedEx.IsCollapsed.ToString(),
                            (!collapsedEx.IsCollapsed).ToString());

                        collapsedEx.IsCollapsed = !collapsedEx.IsCollapsed;

                        mi.Add(actIsCollapsed);
                        curEx.Editor.Do(mi);
                        return;
                    }
                }
            }

            // 普通公式，看能否选中尾子面板。
            if (curEx.SubPanels != null && curEx.SubPanels.Count > 0)
            {
                if (curEx.LastSubPanel != null)
                {
                    curEx.Editor.SelectedExpression = curEx.LastSubPanel;
                    if (curEx.LastSubPanel.SkipSelect)
                    {
                        SelectBackInOrClearBaseText();//跳过子面板。
                    }
                }
            }
            else
            {
                if (curEx is Expressions.BaseText)
                {
                    Command.BaseTextClearText();//如果是文本块，则清除文本。
                }
            }
        }
    }
}
